package com.team7.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.team7.dao.*;
import com.team7.entity.*;
import com.team7.service.CourseListService;
import com.team7.vo.*;
import org.apache.commons.lang.xwork.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * @program: team7-object
 * @description:
 * @author: 田宏洋
 * @create: 2021-10-19 20:09
 **/
@Service
public class CourseListServiceImpl extends ServiceImpl<CourseDao,Course> implements CourseListService {
    @Resource
    private CourseDao courseDao;
    @Resource
    private CourseSubjectDao courseSubjectDao;
    @Resource
    private CourseDescriptionDao courseDescriptionDao;
    @Resource
    private TeacherDao teacherDao;
    @Resource
    private CourseCheckDao courseCheckDao;
    @Override
    public CommonResult findCourseListByPage(Integer page, Integer limit, CourseVo courseVo, String id) {
        Page<CourseVo> objectPage = new Page<>(page,limit);
        QueryWrapper<CourseVo> objectQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(courseVo.getTitle())){
            objectQueryWrapper.like("title",courseVo.getTitle());
        }
        if(StringUtils.isNotEmpty(courseVo.getStatus())){
            objectQueryWrapper.like("status",courseVo.getStatus());
        }
        Page<CourseVo> courseWithPass = courseDao.findCourseWithPass(objectPage, objectQueryWrapper, id);
        List<CourseVo> records = courseWithPass.getRecords();
        List<Subject> subjects = courseSubjectDao.selectList(null);
        for (CourseVo c:records) {
            String s="";
            String s1 = findcourseSubjectName(subjects, c.getSubjectId());
            String[] split = s1.split(">");
            for (int i = split.length-1; i >=0 ; i--) {
                if(i!=0)
                    s += split[i]+">";
                    else
                    s += split[i];
            }
            c.setSubjectName(s);
        }

        return new CommonResult(200,"查询成功",courseWithPass);
    }

    @Override
    public CommonResult updateCourse(UpdateCoursesVo updateCoursesVo) {
        int i = courseDao.updateCourse(updateCoursesVo);
        if (i>0){
            return new CommonResult(200,"修改成功","yes");
        }else {
            return new CommonResult(500,"修改失败","no");
        }

    }

    @Override
    public CommonResult changeStatus(CourseVo courseVo) {
        int i = courseDao.changeStauts(courseVo.getId(), courseVo.getStatus());
        if (i>0){
            return new CommonResult(200,"修改成功","yes");
        }else {
            return new CommonResult(500,"修改失败","no");
        }
    }



    @Override
    public CommonResult updateCourseSubject(CourseUpVo courseUpVo) {
        Course course = new Course();
        course.setTitle(courseUpVo.getTitle());
        course.setCover(courseUpVo.getCover());
        if(courseUpVo.getIsFree()==1){
            course.setPrice(BigDecimal.ZERO);
        }else{
            course.setPrice(courseUpVo.getPrice());
            
        }
        if(StringUtils.isNotEmpty(courseUpVo.getCategoryId3())){
            course.setSubjectId(courseUpVo.getCategoryId2());
            course.setSubjectParentId(courseUpVo.getCategoryId2());
        }else {
            course.setSubjectId(courseUpVo.getCategoryId3());
            course.setSubjectParentId(courseUpVo.getCategoryId1());
        }
        if(StringUtils.isNotEmpty(courseUpVo.getCategoryId2())){
            course.setSubjectId(courseUpVo.getCategoryId2());
            course.setSubjectParentId(courseUpVo.getCategoryId1());
        }
        CourseDescription courseDescription = new CourseDescription();
        if(StringUtils.isNotEmpty(courseUpVo.getId())){
            courseDescription.setId(courseUpVo.getId());
            courseDescription.setDescription(courseUpVo.getDescription());
            courseDescription.setGmtModified(LocalDateTime.now().toString());
            courseDescriptionDao.updateById(courseDescription);
            course.setId(courseUpVo.getId());
            course.setGmtModified(LocalDateTime.now().toString());
            courseDao.updateById(course);
            return new CommonResult(2000,"修改课程成功",null);
        }
        QueryWrapper<Teacher> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("mobile",courseUpVo.getMobile());
        Teacher teacher = teacherDao.selectOne(objectQueryWrapper);
        QueryWrapper<Course> objectQueryWrapper1 = new QueryWrapper<>();
        objectQueryWrapper1.eq("title",courseUpVo.getTitle());
        objectQueryWrapper1.eq("teacher_id",teacher.getId());
        Course course1 = courseDao.selectOne(objectQueryWrapper1);
        if(course1!=null){
            return new CommonResult(5000,"该课程已经存在",null);
        }
        course.setTeacherId(teacher.getId());

        course.setGmtCreate(LocalDateTime.now().toString());
        course.setGmtModified(LocalDateTime.now().toString());
        int insert = courseDao.insert(course);
        QueryWrapper<Course> objectQueryWrapper2 = new QueryWrapper<>();
        objectQueryWrapper2.eq("title",courseUpVo.getTitle());
        Course course2 = courseDao.selectOne(objectQueryWrapper2);
        CourseCheck courseCheck = new CourseCheck();
        courseCheck.setCourseId(course2.getId());
        int insert1 = courseCheckDao.insert(courseCheck);
        courseDescription.setId(course2.getId());
        courseDescription.setDescription(courseUpVo.getDescription());
        courseDescription.setGmtCreate(LocalDateTime.now().toString());
        courseDescription.setGmtModified(LocalDateTime.now().toString());

        courseDescriptionDao.insert(courseDescription);
        if(insert>0&&insert1>0){
            return new CommonResult(2000,"新增课程成功",course2.getId());

        }
        return new CommonResult(5000,"新增课程失败",null);
    }

    @Override
    public Integer updateIsAdd(String id) {
        Course course = new Course();
        QueryWrapper<Course> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("id",id);
        Course course1 = courseDao.selectOne(objectQueryWrapper);
        if(course1.getIsAdd()==0){
            course1.setIsAdd(1);
            course1.setId(id);
            int i = courseDao.updateById(course1);
            return i;
        }else{
            course1.setIsAdd(0);
            course1.setId(id);
            int i = courseDao.updateById(course1);
            return i;
        }
    }

    @Override
    public CommonResult getCoursByTeacherPhone(Integer page, Integer limit, String mobile, String checkStatus) {
        Page<Course> objectPage = new Page<>(page,limit);
        QueryWrapper<Teacher> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("mobile",mobile);
        Teacher teacher = teacherDao.selectOne(objectQueryWrapper);
        if("8".equals(checkStatus)){
            checkStatus="";
        }
        Page<Course> courseByTeacherMobile = courseDao.findCourseByTeacherMobile(objectPage, teacher.getId(), checkStatus);

        return new CommonResult(2000,"获取成功",courseByTeacherMobile);
    }

    @Override
    public CommonResult findCourseByTeacherId(Integer page, Integer limit, TeacherCourseVo teacherCourseVo) {
        Page<Course> objectPage = new Page<>(page,limit);
        QueryWrapper<Course> objectQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(teacherCourseVo.getTeacherId())){
            objectQueryWrapper.eq("teacher_id",teacherCourseVo.getTeacherId());
        }
        if(StringUtils.isNotEmpty(teacherCourseVo.getCourseName())){
            objectQueryWrapper.like("title",teacherCourseVo.getCourseName());
        }
        Page<Course> courseByTeacherId = courseDao.findCourseByTeacherId(objectPage, objectQueryWrapper);
        return new CommonResult(2000,"查询成功",courseByTeacherId);
    }

    @Override
    public CommonResult findFontCourseByPage(Integer page, Integer limit, FontCourseVo courseVo) {
        Page<CourseVo> objectPage = new Page<>(page,limit);
        QueryWrapper<CourseVo> objectQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(courseVo.getTwoSubjectId())){
            objectQueryWrapper.eq("subject_id",courseVo.getTwoSubjectId());
        }
        if(StringUtils.isNotEmpty(courseVo.getOneSubjectId())){
            objectQueryWrapper.eq("subject_parent_id",courseVo.getOneSubjectId());
        }
        if(StringUtils.isNotEmpty(courseVo.getIsFee())){
            if(courseVo.getIsFee().equals("0")){
                objectQueryWrapper.eq("price",courseVo.getIsFee());
            }else {
                objectQueryWrapper.gt("price","0");
            }
        }
        this.courseDao.FondfindCourseWithPass(objectPage,objectQueryWrapper);
        return new CommonResult(2000,"查询成功",objectPage);
    }

    @Override
    public CommonResult findTeacherByTeacherId(String teacherId) {
        Teacher teacher = teacherDao.selectById(teacherId);
        return new CommonResult(2000,"获取成功",teacher);
    }

    public static String findcourseSubjectName(List<Subject> subjects,String id){
        String name = "";
        for (Subject subject : subjects) {
            if (id.equals(subject.getId())){
                name += "【"+subject.getTitle()+"】"+">";
                if (!subject.getParentId().equals("0")){
                    String name1 = findcourseSubjectName(subjects, subject.getParentId());
                    name +=name1;
                }
            }
        }
        return name;
    }

}
